package com.zhanglu.fun.services.authentication.role;

import com.zhanglu.fun.services.authentication.bean.ApplicationRole;
import com.zhanglu.fun.services.authentication.bean.AuthToken;
import com.zhanglu.fun.services.authentication.client.DataCenterClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @description
 * @Auther zhanglu
 * @Date 2017/9/21 下午2:19
 */
@Service
@Slf4j
public class RoleService {
    @Autowired
    private DataCenterClient dataCenterClient;

    public List<String> getApplicationRolesByToken(String token) {
        AuthToken authToken = dataCenterClient.getAuthTokenByToken(token);
        if (Objects.isNull(authToken)) {
            return null;
        }
        List<String> roles = dataCenterClient.getApplicationRolesByUserId(authToken.getUserId()).
                stream().map(ApplicationRole::getRoleName).
                filter(Objects::nonNull).collect(Collectors.toList());
        log.debug("query ApplicationRole result:[userId is: {}|it has roles: {}]", authToken.getUserId(), roles);
        return roles;
    }
}
